<!-- src/views/user/RoleEdit.vue -->

<script setup>
import { ref, reactive } from 'vue';
import { useRoleStore } from '@/stores/roleStore.js';
import RoleList from "@/components/RoleList/Index.vue";
import {ElMessage} from "element-plus";

const roleStore = useRoleStore();

const dialogVisible = ref(false);

const role = reactive({
  roleId:'',
  roleIds: [],
  name: '',
  code: '',
  dataScope: 1,
  aclIds: [],
  description: ''
});

const rules = {
  name: [{ required: true, message: '请输入角色名称', trigger: 'blur' }],
  code: [{ required: true, message: '请输入角色编码', trigger: 'blur' }],
  dataScope: [{ required: true, message: '请选择数据权限范围', trigger: 'change' }]
};

const roleForm = ref(null);

const submitForm = () => {
  roleForm.value?.validate().then( () => {
    console.log("校验通过", role.value);
    roleStore.updateRole({
      ...role
    })

  }).catch( () => {
    console.log("校验失败");
  })
  console.log("角色信息:", role);

  close();

}


const show = (roleInfo) => {
  console.log("校验通过", roleInfo); // 检查 roleInfo 是否包含 id
  Object.assign(role, roleInfo);
  role.roleId =roleInfo.id;
  console.log("角色信息:", role); // 检查 role 是否正确接收 id
  dialogVisible.value = true;
};

const close = () => {
  dialogVisible.value = false;
};

// 对外暴露show
defineExpose({
  show
});
</script>

<template>
  <el-dialog
      title="角色编辑"
      v-model="dialogVisible"
      width="50%"
  >
    <el-form :model="role" :rules="rules" ref="roleForm" label-width="100px">
      <el-form-item label="角色名称" prop="name">
<!--       <RoleList v-model="role.roleIds" :roleIds="role.roleIds"></RoleList>-->
        <el-input v-model="role.name" placeholder="请输入角色编码"></el-input>
      </el-form-item>
      <el-form-item label="角色编码" prop="code">
        <el-input v-model="role.code" placeholder="请输入角色编码"></el-input>
      </el-form-item>
      <el-form-item label="数权限范围" prop="dataScope">
        <el-select v-model="role.dataScope" placeholder="请选择数据权限范围">
          <el-option label="全部" :value="1"></el-option>
          <el-option label="本部门" :value="2"></el-option>
          <el-option label="自定义" :value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="角色描述" prop="description">
        <el-input v-model="role.description" placeholder="请输入角色描述"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">确定</el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<style scoped>

</style>
